//
//  ShuaWordGenDuViewController.m
//  Learn
//
//  Created by apple on 2024/4/19.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "ShuaWordGenDuViewController.h"
#import "ShuaWordRequest.h"

@interface ShuaWordGenDuViewController ()


@property (nonatomic, strong) UIView    * proBgView;
@property (nonatomic, strong) UIView    * proView;
@property (nonatomic, strong) UILabel    * proLabel;


@property (nonatomic, strong) UIButton    * preBtn;
@property (nonatomic, strong) UIButton    * nextBtn;
@property (nonatomic, strong) UIButton    * voiceBtn;

@property (nonatomic, strong) UIImageView    * logoImg;

@property (nonatomic, strong) UILabel    * wordLabel;
@property (nonatomic, strong) UILabel    * yinbiaoLabel;
@property (nonatomic, strong) UILabel    * expLabel;


@property (nonatomic, strong) UILabel    * lijuLabel;
@property (nonatomic, strong) UILabel    * lijuEnLabel;
@property (nonatomic, strong) UILabel    * lijuZhLabel;

@property (nonatomic, assign) NSInteger            index;       //

/* <#描述#> */
@property (nonatomic, strong) ShuaWordModel    * model;
@end

@implementation ShuaWordGenDuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = UIColor.whiteColor;
  
    self.title = @"单词跟读练习";
    self.index = 0;
    [self initUI];
    if (self.dataArray.count > 0) {
        self.model = self.dataArray[self.index];
        [self updateUIWithModel:self.model];
    }    
}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)initUI {
    
    [self.view addSubview:self.proLabel];
    [self.view addSubview:self.proBgView];
    [self.view addSubview:self.proView];
    
    
    [self.view addSubview:self.logoImg];

    [self.view addSubview:self.wordLabel];
    [self.view addSubview:self.yinbiaoLabel];
    [self.view addSubview:self.expLabel];
    
    [self.view addSubview:self.lijuLabel];
    [self.view addSubview:self.lijuEnLabel];
    [self.view addSubview:self.lijuZhLabel];

}

- (UIView *)proBgView {
    if(!_proBgView){
        _proBgView = [[UIView alloc]initWithFrame:CGRectMake(48, 21 + NavigationBarHeight, SCREEN_W - 58, 2)];
        
        _proBgView.backgroundColor = rgbHex(0xe5e5e5, 1);
        [_proBgView js_setRoundedCorners:UIRectCornerAllCorners radii:1];
    }
    return _proBgView;
}
- (UIView *)proView {
    if(!_proView){
        _proView = [[UIView alloc]initWithFrame:CGRectMake(self.proBgView.left, self.proBgView.top, self.proBgView.width*(1/self.dataArray.count), 2)];
        
        _proView.backgroundColor = COLOR_TOPIC_SELECT;
        [_proView js_setRoundedCorners:UIRectCornerAllCorners radii:1];
    }
    return _proView;
}

- (UILabel *)proLabel {
    if(!_proLabel){
        _proLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10 + NavigationBarHeight, 25, 22)];
        _proLabel.textColor = COLOR_NOT_SELECT;
        _proLabel.font = PingFangMediumFont(16);
        _proLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _proLabel;
}

- (void)setproNum:(NSInteger)index {
    CGFloat leftLength = 0;
    
    NSString *leftNum = [NSString stringWithFormat:@"%li",(long)index];
    NSString *rightNum = [NSString stringWithFormat:@"/%lu",(unsigned long)self.dataArray.count];
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@",leftNum,rightNum]];
    
    [attributeStr addAttribute:NSForegroundColorAttributeName
                         value:COLOR_TOPIC_SELECT
                         range:NSMakeRange(leftLength, leftNum.length)];
    leftLength += leftNum.length;
    
    [attributeStr addAttribute:NSForegroundColorAttributeName
                         value:COLOR_NOT_SELECT
                         range:NSMakeRange(leftLength,rightNum.length )];
    self.proLabel.attributedText = attributeStr;
}



- (UILabel *)lijuZhLabel {
    if(!_lijuZhLabel){
        _lijuZhLabel = [[UILabel alloc]initWithFrame:CGRectMake(28, self.lijuEnLabel.bottom + 5, SCREEN_W - 56, 22)];
        _lijuZhLabel.centerX = self.expLabel.centerX;

//        _lijuZhLabel.text = @"我是两天前来这儿的";
        _lijuZhLabel.textColor = COLOR_NOT_SELECT;
        _lijuZhLabel.font = PingFangMediumFont(14);
        _lijuZhLabel.textAlignment = NSTextAlignmentCenter;
        _lijuZhLabel.hidden = YES;
    }
    return _lijuZhLabel;
}
- (UILabel *)lijuEnLabel {
    if(!_lijuEnLabel){
        _lijuEnLabel = [[UILabel alloc]initWithFrame:CGRectMake(28, self.lijuLabel.bottom + 10, SCREEN_W - 56, 22)];
        _lijuEnLabel.centerX = self.expLabel.centerX;

//        _lijuEnLabel.text = @"I came here two days ago..";
        _lijuEnLabel.textColor = COLOR_NOT_SELECT;
        _lijuEnLabel.font = PingFangMediumFont(14);
        _lijuEnLabel.textAlignment = NSTextAlignmentCenter;
        _lijuEnLabel.hidden = YES;
    }
    return _lijuEnLabel;
}
- (UILabel *)lijuLabel {
    if(!_lijuLabel){
        _lijuLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.expLabel.bottom + 20, 100, 22)];
        _lijuLabel.centerX = self.expLabel.centerX;
        _lijuLabel.text = @"例句";
        _lijuLabel.textColor = COLOR_NOT_SELECT;
        _lijuLabel.font = PingFangMediumFont(14);
        _lijuLabel.textAlignment = NSTextAlignmentCenter;
        _lijuLabel.hidden = YES;
    }
    return _lijuLabel;
}


- (UIImageView *)logoImg {
    if(!_logoImg){
        _logoImg = [[UIImageView alloc]initWithFrame:CGRectMake(28, 39 + self.proView.bottom, SCREEN_W - 56,0.56 *(SCREEN_W - 56))];
        _logoImg.contentMode = UIViewContentModeScaleAspectFill;
        [_logoImg js_setRoundedCorners:UIRectCornerAllCorners radii:10];
//        _logoImg.image = [UIImage imageNamed:@"planBg"];
    }
    return _logoImg;
}

- (UILabel *)expLabel {
    if(!_expLabel){
        _expLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10 + self.yinbiaoLabel.bottom, SCREEN_W, 25)];
//        _expLabel.text = @"adv.以前";
        _expLabel.textColor = COLOR_NOT_SELECT;
        _expLabel.font = PingFangMediumFont(18);
        _expLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _expLabel;
}
- (UILabel *)yinbiaoLabel {
    if(!_yinbiaoLabel){
        _yinbiaoLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10 + self.wordLabel.bottom, SCREEN_W, 25)];
//        _yinbiaoLabel.text = @"[a'gau]";
        _yinbiaoLabel.textColor = COLOR_NOT_SELECT;
        _yinbiaoLabel.font = PingFangMediumFont(18);
        _yinbiaoLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _yinbiaoLabel;
}
- (UILabel *)wordLabel {
    if(!_wordLabel){
        _wordLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20 + self.logoImg.bottom, SCREEN_W, 45)];
//        _wordLabel.text = @"ago";
        _wordLabel.textColor = COLOR_BLACK;
        _wordLabel.font = PingFangMediumFont(32);
        _wordLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _wordLabel;
}

- (UIButton *)voiceBtn {
    if(!_voiceBtn){
        _voiceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _voiceBtn.frame = CGRectMake(SCREEN_W - 40, NavigationBarHeight, 40, 40);
        
        [_voiceBtn setImage:[UIImage imageNamed:@"voiceIcon"] forState:UIControlStateNormal];
        [_voiceBtn addTarget:self action:@selector(voiceBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _voiceBtn;
}


- (UIButton *)preBtn {
    if(!_preBtn){
        _preBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _preBtn.frame = CGRectMake(10, SCREEN_H - SafeAreaBottomHeight - 55, SCREEN_W/2 - 35/2, 45);
        _preBtn.backgroundColor = UIColor.whiteColor;
        [_preBtn setTitle:@"重读" forState:UIControlStateNormal];

        
        [_preBtn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
        _preBtn.titleLabel.font  = PingFangMediumFont(18);
      
        [_preBtn js_setRoundedCorners:UIRectCornerAllCorners radii:22.5 borderColor:UIColor.blackColor width:1];
        [_preBtn addTarget:self action:@selector(preBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _preBtn;
}
- (UIButton *)nextBtn {
    if(!_nextBtn){
        _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _nextBtn.frame = CGRectMake(15 + self.preBtn.right, SCREEN_H - SafeAreaBottomHeight - 55, self.preBtn.width, 45);
        [_nextBtn setTitle:@"下一个" forState:UIControlStateNormal];

        
        [_nextBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _nextBtn.titleLabel.font  = PingFangMediumFont(18);
        _nextBtn.az_colors = @[(__bridge id)rgbHex(0x02A1F9, 1).CGColor,(__bridge id)rgbHex(0x29F19C,1).CGColor];
        _nextBtn.az_startPoint = CGPointMake(0, 0.5);
        _nextBtn.az_endPoint = CGPointMake(1, 0.5);
        [_nextBtn js_setRoundedCorners:UIRectCornerAllCorners radii:22.5];
        [_nextBtn addTarget:self action:@selector(nextBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nextBtn;
}

#pragma mark   底部按钮点击
- (void)preBtnClicked:(UIButton *)sender {
   
}
- (void)nextBtnClicked:(UIButton *)sender {
    if (self.index == self.dataArray.count - 1) {
        [SVProgressHUD showImage:nil status:@"已是最后一个"];
        return;
    }
    self.index ++;
    self.model = self.dataArray[self.index];
    [self updateUIWithModel:self.model];
}


#pragma mark   播放
- (void)voiceBtnClicked:(UIButton *)sender {
    
}
- (void)voice2BtnClicked:(UIButton *)sender {
    
}

- (void)getRequestData{
   
}
- (void)updateUIWithModel:(ShuaWordModel*)model {
    [self setproNum:self.index + 1];
    self.wordLabel.text = model.words_detail.words;
    self.yinbiaoLabel.text = [NSString stringWithFormat:@"[%@]",model.words_detail.symbol];
    self.expLabel.text = model.words_detail.pretations;
    [self.logoImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.words_detail.wordPic]]];
    
  self.lijuLabel.hidden = self.lijuEnLabel.hidden = self.lijuZhLabel.hidden = YES;
    

}

@end
